using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ClassBuilderWizard.Interfaces;

namespace ClassBuilderWizard.DefaultPlugins.Forms
{
    public partial class frmMSSQLWizardStep2 : WizardFormBase
    {
        private string _DBConn;
        private Parameters _Pars;

        public frmMSSQLWizardStep2(string dbConn, Parameters pars)
        {
            InitializeComponent();

            this._DBConn = dbConn;
            this._Pars = pars;
        }

        public string DatabaseObjectName
        {
            get { return tvDBObject.SelectedNode.Text; }
        }
        public string DBConn
        {
            get { return this._DBConn; }
            set { this._DBConn = value; }
        }
        public DatabaseObject SelectedObject
        {
            get { return (DatabaseObject)propertyGrid1.SelectedObject; }
        }

        private void Bind()
        {
            SMOHelper smoh = new SMOHelper(this._DBConn, this._Pars);

            try
            {
                TreeNode tables = tvDBObject.Nodes[0].Nodes[0]; //the Tables node
                tables.Nodes.Clear();
                string[] tableNames = smoh.TableList();
                foreach (string tableName in tableNames)
                {
                    //iterate through each of the tables in the database and add them to the tree view
                    if (tableName.Trim() != string.Empty)
                    {
                        TreeNode tn = new TreeNode(tableName);
                        tn.ImageIndex = 1;
                        tn.SelectedImageIndex = 1;
                        
                        DatabaseObject p = new DatabaseObject(this._Pars.CurrentProjectType);
                        string name = tableName.Substring(tableName.IndexOf('.') + 1);
                        string schema = tableName.Substring(0, tableName.IndexOf('.'));

                        p.Name = name;
                        p.Schema = schema;
                        p.Type = DatabaseObjectType.Table;

                        tn.Tag = p;

                        tables.Nodes.Add(tn);
                    }
                }

                TreeNode views = tvDBObject.Nodes[0].Nodes[1]; // the Views node
                views.Nodes.Clear();
                string[] viewNames = smoh.ViewList();
                foreach (string viewName in viewNames)
                {
                    //iterate through each of the views and add it to the tree view
                    TreeNode tn = new TreeNode(viewName);
                    tn.ImageIndex = 2;
                    tn.SelectedImageIndex = 2;

                    DatabaseObject p = new DatabaseObject(this._Pars.CurrentProjectType);
                    string name = viewName.Substring(viewName.IndexOf('.') + 1);
                    string schema = viewName.Substring(0, viewName.IndexOf('.'));

                    p.Name = name;
                    p.Schema = schema;
                    p.Type = DatabaseObjectType.View;

                    tn.Tag = p;

                    views.Nodes.Add(tn);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("There was a problem connecting to and retrieving information from the database. Please click the '< Back' button and check the connection string.\nDetails: " + ex.ToString());
            }
        }

        private void tvDBObject_AfterSelect(object sender, TreeViewEventArgs e)
        {
            bool enable = false;
            if (tvDBObject.SelectedNode != null)
            {
                //enable if the selected node is not the root, the tables, or the views node
                enable = (tvDBObject.SelectedNode != tvDBObject.Nodes[0] && tvDBObject.SelectedNode != tvDBObject.Nodes[0].Nodes[0] && tvDBObject.SelectedNode != tvDBObject.Nodes[0].Nodes[1]);
            }

            if (enable)
            {
                //the selected node is a table or a view
                TreeNode tn = tvDBObject.SelectedNode;
                DatabaseObject props = (DatabaseObject)tn.Tag;
                //get the column list for the database object
                if (props.ColumnCount == 0)
                {
                    SMOHelper smoh = new SMOHelper(this._DBConn, this._Pars);
                    props.Columns = (props.Type == DatabaseObjectType.Table) ? smoh.TableColumnList(props.Name, props.Schema) : smoh.ViewColumnList(props.Name, props.Schema);
                    props.KeyColumns = new List<DatabaseColumn>(props.Columns);
                }
                propertyGrid1.SelectedObject = props;
            }

            btnFinish.Enabled = enable;
        }

        private void frmMSSQLWizardStep2_Load(object sender, EventArgs e)
        {
            Bind();
        }
    }
}